import QtQuick
import QtQuick.Controls
import QtQuick.Controls.Material
import EcgModel 1.0

Rectangle{
    id: ecgFragment
    anchors.fill: parent
    color: "#ffffff"
    property int duration: 10*1000

    Rectangle{
        id:fragmentEcg
        width: parent.width
        height:parent.height-10
        EcgFragmentBeatsInfo{
            id:ecgFragmentBeatsInfo
            start: 0
            duration: ecgFragment.duration
            padding:[120,0,40,0] //80 0 40 0
        }
        EcgCanvas {
            id: ecgContent
            start: 0
            duration: ecgFragment.duration
            padding:[120,0,40,0] //80 0 40 0
        }
        EcgGrid{
            duration: ecgFragment.duration
        }
        MouseArea {
            id: selectionArea
            cursorShape:"CrossCursor"
            anchors.fill: parent
            property real startX: 0
            property real startY: 0
            property bool selecting: false
            property int sampleRate: EcgModel.sampleRate
            property int signalSize: EcgsignalModel.getSignalSize()

            onPressed: function(mouse)  {
                startX = mouse.x
                startY = mouse.y
                selecting = true
                selectionRect.visible = true
                selectionRect.x = startX
                selectionRect.y = startY
                selectionRect.width = 0
                selectionRect.height = 0
            }

            onPositionChanged:function (mouse) {
                if (selecting) {
                    var width = mouse.x - startX
                    var height = mouse.y - startY
                    selectionRect.x = width < 0 ? startX + width : startX
                    selectionRect.y = height < 0 ? startY + height : startY
                    selectionRect.width = Math.abs(width)
                    selectionRect.height = Math.abs(height)
                    //识别框住的心搏
                    let ecgDuration = ecgContent.duration
                    var start = ecgContent.start>=0 ? ecgContent.start:0
                    var selsctStart = start+ selectionRect.x /(selectionArea.width / (ecgDuration/1000*sampleRate))
                    var selsctDuration = (selectionRect.width / selectionArea.width) * ecgDuration
                    //所有选中的心搏
                    var rPeaksInAlls = beatsModel.getFragmentBeats(selsctStart,selsctDuration)
                    //设置为选中状态
                    if(rPeaksInAlls.length > 0){
                        var arr = beatsModel.setSelectedBeats(rPeaksInAlls) //返回所有之前选中的心搏
                        // console.log(count)
                        fragmentBeatsList.refreshMData([...rPeaksInAlls,...arr])
                        gridViewBeatsList.refreshMData([...rPeaksInAlls,...arr])
                    }
                }
            }

            onReleased: function (mouse){
                selecting = false
                selectionRect.visible = false
            }
            onWheel:function(wheel) {
                if (wheel.angleDelta.y > 0) {
                    let newValue = fragmentEcgSlider.value - 1*sampleRate
                    if(newValue < 0) newValue = 0
                    fragmentEcgSlider.value = newValue
                } else {
                    let newValue = fragmentEcgSlider.value + 1*sampleRate
                    if(newValue > fragmentEcgSlider.to) newValue = fragmentEcgSlider.to
                    fragmentEcgSlider.value = newValue
                }

            }
        }

        Rectangle {
            id: selectionRect
            border.color : "red"
            color: "transparent"
            border.width: 1
            visible: false
        }
        Component.onCompleted: {
            // ecgContent.start =2000;
            // console.log(EcgModel.startTimeStamp)
        }
    }
    Slider {
        id: fragmentEcgSlider
        width: parent.width
        height: 10
        anchors.bottom: parent.bottom
        anchors.left: parent.left
        from: 0
        to: 1000
        value: 0
        stepSize: 1
        padding:0
        focusPolicy:Qt.NoFocus
        background: Rectangle {
            x: fragmentEcgSlider.leftPadding + fragmentEcgSlider.visualPosition * (fragmentEcgSlider.availableWidth - width)
            y: 0
            width: fragmentEcgSlider.availableWidth
            height: 10
            color: "#efefef"
        }
        handle: Rectangle {
            x: fragmentEcgSlider.leftPadding + fragmentEcgSlider.visualPosition * (fragmentEcgSlider.availableWidth - width)
            y: 0
            width: 50
            height: 10
            radius: 2
            color: "#525252"
        }
        Keys.onPressed:function(event) {
            event.accepted = true
        }
        onValueChanged: {
            ecgContent.start = value
            ecgFragmentBeatsInfo.start = value
        }
        Component.onCompleted: {
            fragmentEcgSlider.to = EcgsignalModel.getSignalSize() - ecgContent.duration/1000 * EcgModel.sampleRate
        }
    }

    Connections {
        target: myBeatsGrid
        function onRPeaksInAllChenged (rPeaksInAll) {
            ecgContent.start = rPeaksInAll - ecgContent.duration/1000/2 * EcgModel.sampleRate
            ecgFragmentBeatsInfo.start = rPeaksInAll - ecgContent.duration/1000/2 * EcgModel.sampleRate
            fragmentEcgSlider.value = ecgFragmentBeatsInfo.start
        }
    }
}
